﻿<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="my.myorm.tool.jdbc.SQLExecutor" %>
<%@ page import="my.myorm.tool.jdbc.SQLManager" %>
<%@ page import="java.util.Date" %>
<%@ page import="my.myorm.tool.jdbc.ResultList" %>
<%@ page import="com.mtbu.bean.SysRole" %>
<%@ page import="com.util.SuperString" %>
<%@ page import="com.mtbu.dao.DAOManager" %>
<%@ page import="com.mtbu.bean.SysUser" %>
<%@ page import="com.mtbu.comm.Util" %>
<%@ page import="com.util.SuperPage" %>
<%@ page import="com.mtbu.bean.SysFunc" %>
<%@page import="com.mtbu.bean.SysFunc"%>
<%@page import="com.mtbu.bean.SysFuncMap"%>
<%@page import="com.mtbu.dao.SysFuncMapDao"%>
<%@page import="com.mtbu.service.LogUtil"%>
<%@ include file="sys_commcheck.jsp" %>
<%!
    public static void addFuncToUR(int fid,String[] arrids_str, SQLExecutor dbcon) throws Exception {
		for(String strid : arrids_str) {
			int type=-1;
	        String _name = "";
	        if(strid.startsWith("role_")) {
	        	type = 1;
	        	_name = SuperString.notNullTrim(strid.substring("role_".length()));
	        } else {
	        	type = 0;
	        	_name = SuperString.notNullTrim(strid);
	        }
	        SysFuncMap sfmap = new SysFuncMap();
	        sfmap.setFID(fid);
	        sfmap.setType(type);
	        sfmap.setURName(_name);
	        DAOManager.sysFuncMapDao.save(sfmap);
	    }
	}
    public static void deleteFuncFromUR(int fid, SQLExecutor dbcon) throws Exception {
        String sql = "delete from sys_func_map where fid=?";
        dbcon.addParam(fid);
        dbcon.execute(sql);
    }
%>
<%
    if(!Constant_isAdmin) {
        Util.callInfo("没有操作权限！", request, response);
        return;
    }

    String act = SuperString.notNullTrim(request.getParameter("act"));
    if(!act.equalsIgnoreCase("mod") && !act.equalsIgnoreCase("del")) act = "add";
    boolean isAdd = act.equalsIgnoreCase("add");
    boolean isMod = act.equalsIgnoreCase("mod");
    boolean isDel = act.equalsIgnoreCase("del");
    String curFile = SuperPage.getCurURLFile(request);
    String backFile = SuperString.replace(curFile, "funcmoddo", "");
    String curName = "系统功能";

    int id = SuperString.getInt(request.getParameter("id"));
    String funcid = SuperString.notNullTrim(request.getParameter("funcid"));
    String funcid_old = SuperString.notNullTrim(request.getParameter("funcid_old"));
    String funcname = SuperString.notNullTrim(request.getParameter("funcname"));
    String mid = SuperString.notNullTrim(request.getParameter("mid"));

    String[] arrids_str = request.getParameterValues("check_id");
    if(arrids_str == null) arrids_str = new String[]{};

    if(!isDel && ("".equals(funcid) || "".equals(funcname))) {
        Util.callInfo("请填写完整！", request, response);
        return;
    }
    if(isDel && id <= 0) {
        Util.callInfo("错误的调用方法！", request, response);
        return;
    }

    SQLExecutor dbconn = SQLManager.getSQLExecutor();
    String sql = "";

    if((isAdd || isMod) && !funcid.equalsIgnoreCase(funcid_old)) {
        sql = "select id from sys_func where ModuleID=? and funcid=? and funcid<>?";
        dbconn.addParam(mid);
        dbconn.addParam(funcid);
        dbconn.addParam(funcid_old);
        SysFunc func = dbconn.queryForBean(sql, SysFunc.class);
        if(func != null) {
            Util.callInfo("该功能ID已经存在，请重新填写！", request, response);
            return;
        }
    }

    SysFunc func = new SysFunc();
    if(isAdd) {
        func.setFuncID(funcid);
        func.setFuncName(funcname);
        func.setModuleID(mid);
        DAOManager.sysFuncDao.save(func);
    }

    if(isMod) {
        func.setId(id);
        func.setFuncID(funcid);
        func.setFuncName(funcname);
        func.setModuleID(mid);
        DAOManager.sysFuncDao.update(func);
    }
	
    deleteFuncFromUR(func.getId(), dbconn);
    addFuncToUR(func.getId(),arrids_str, dbconn);    

    if(isDel) {
        SysFunc delfunc = new SysFunc();
        delfunc.setId(id);
        DAOManager.sysFuncDao.delete(delfunc);

        //将功能从权限表中删除
        if(funcid.indexOf("-")>0) deleteFuncFromUR(id, dbconn);
    }

    LogUtil.writeLog(Constant_UserName, Util.getActText(act) + curName,
            id + "|" + funcname, request, null);

    response.sendRedirect(backFile + SuperPage.getQueryQ(request, "act,id"));
    return;
%>